﻿using GenerateCode.IService;
using GoodDotNet.Core.Data;
using GoodDotNet.IService;
using GoodDotNet.Service;

namespace GenerateCode.Service
{
    public class CreateEntityService : IServiceCreateEntity
    {

        private string table_name;
        private string entity_namespace;
        private string repository_namespace;
        private string iRepository_namespace;
        private ConnectionString connectionString;
        public CreateEntityService(ConnectionString conn)
        {
            connectionString = conn;
        }
        public CreateEntityService TableName(string table_name) {
            this.table_name = table_name;
            return this;
        }
        public CreateEntityService Entity( string name_space, string save_path)
        {
           
            entity_namespace = name_space;
            using (var unitOfWork = new UnitOfWork(connectionString))
            {
                var key_name = unitOfWork.DataRepository.queryTablePrimary(table_name);
                var list = unitOfWork.DataRepository.QueryTable(table_name);
                if (list == null)
                {
                    Console.WriteLine("返回空值");
                }
                else
                {
                    String FileName = table_name.Substring(0, 1).ToUpper() + table_name.Substring(1, table_name.Length - 1).ToLower();
                    string path = Path.Combine(save_path, FileName + ".cs");
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }
                    string text = @"using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace {0}
{{
    {1}
    public class {2}  
    {{
 {3}
     }}
 }}
 ";

                    string tab = @"   ";
                    string colume = "";
                    foreach (var item in list)
                    {
                        colume += Environment.NewLine;
                        colume += tab + tab + tab + @"/// <summary>" + Environment.NewLine;
                        colume += tab + tab + tab + "/// " + item.COLUMN_COMMENT + Environment.NewLine;
                        colume += tab + tab + tab + @"/// </summary>" + Environment.NewLine;
                        if (item.COLUMN_NAME.Equals(key_name))
                        {
                            colume += tab + tab + tab + "[Key]" + Environment.NewLine;
                        }
                        colume += "\t\tpublic ";

                        if (item.DATA_TYPE == "string")
                        {
                            colume += " string";
                        }
                        else if (item.DATA_TYPE.Contains("bigint"))
                        {
                            colume += " long";
                        }
                        else if (item.DATA_TYPE.Contains("int") || item.DATA_TYPE.Contains("integer"))
                        {
                            colume += " int";
                        }
                        else if (item.DATA_TYPE.Contains("varchar") || item.DATA_TYPE.Contains("longtext") || item.DATA_TYPE.Contains("json") || item.DATA_TYPE.Contains("char"))
                        {
                            colume += " string";
                        }
                        else if (item.DATA_TYPE.Contains("time") || item.DATA_TYPE.Contains("date"))
                        {
                            colume += " DateTime";
                        }
                        else if (item.DATA_TYPE.Contains("bit"))
                        {
                            colume += " bool";
                        }
                        else if (item.DATA_TYPE.Contains("decimal") || item.DATA_TYPE.Contains("float"))
                        {
                            colume += " float";
                        }
                        else if (item.DATA_TYPE.Contains("double"))
                        {
                            colume += " double";
                        }
                        else
                        {
                            colume += " string";
                        }
                        if (item.IS_NULLABLE.ToLower() == "yes")
                        {
                            colume += "? ";
                        }
                        colume += " " + item.COLUMN_NAME;
                        colume += " { get; set; }" + Environment.NewLine;

                    }
                    string table = "[Table(\"" + table_name + "\")]" + Environment.NewLine;
                    string alltext = String.Format(text, name_space, table, table_name, colume);
                    File.WriteAllText(path, alltext);
                    Console.WriteLine("生成实体文件成功");
                }
            }
            return this;
        }


        public CreateEntityService Repository(string name_space, string save_path)
        {
            String FileName = table_name.Substring(0, 1).ToUpper() + table_name.Substring(1, table_name.Length - 1).ToLower();
            string path = Path.Combine(save_path, FileName + "Repository.cs");
            string repository_name = FileName + "Repository";
            string iRepository_name = FileName + "IRepository";
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            string text = "using " + entity_namespace + ";" + Environment.NewLine;
            text += "using " + iRepository_namespace + ";" + Environment.NewLine;
            text += @"
using GoodDotNet.Core.Repository.Base;

namespace {0}
{{
   public class {1} : BaseRepository<{2}>, {3}
    {{
        public {1}(MyDbContext context) : base(context)
        {{
        }}
    }}
 }}
 ";

            string alltext = String.Format(text, name_space, repository_name, table_name, iRepository_name);
            File.WriteAllText(path, alltext);
            Console.WriteLine("生成实体仓储文件成功");
            return this;
        }

        public CreateEntityService IRepository(string name_space, string save_path)
        {
            this.iRepository_namespace = name_space;
            String FileName = table_name.Substring(0, 1).ToUpper() + table_name.Substring(1, table_name.Length - 1).ToLower();
            string path = Path.Combine(save_path, FileName + "IRepository.cs");
            string repository_name = FileName + "Repository";
            string iRepository_name = FileName + "IRepository";
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            string text = "using " + entity_namespace + ";" + Environment.NewLine;
            text += @"using GoodDotNet.Data.IRepository.BASE;

namespace {0}
{{
   public interface {1} : IBaseRepository<{2}>
    {{
    }}
 }}
 ";

            string alltext = String.Format(text, name_space, iRepository_name, table_name);
            File.WriteAllText(path, alltext);
            Console.WriteLine("生成实体仓储文件成功");
            return this;
        }
    }
}

